package com.goods.shop.util;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;

import java.util.Map;

/**   
*      
* 类描述：   
* 创建人：王金鑫  
* 创建时间：2020年5月31日 下午4:30:30   
* 修改人：王金鑫     
* 修改时间：2020年5月31日 下午4:30:30   
* 修改备注：   
* @version    
*    
*/
public class HttpUtils
{
	public static String requestGet(String url,Map<String, Object> params)
	{
		String result=requestGet(url,null,params);
		return result;
	}
	public static String requestGet(String url, Map<String, String> headers, Map<String, Object> params)
	{
		StringBuilder builder = new StringBuilder(url);
		if(null!=params){
			builder.append("?");
			for (Map.Entry<String, Object> param : params.entrySet()) {
				builder.append(param.getKey()).append("=").append(param.getValue()).append("&");
			}
		}
		String result = "";
		HttpRequest request = HttpRequest.get(builder.toString());
		if(headers!=null){
			for (Map.Entry<String, String> header : headers.entrySet()) {
				request.header(header.getKey(), header.getValue());
			}
		}

		result = request.execute().body();
		return result;
	}
	public static String requestPost(String url, Map<String, Object> params)
	{
		String result = requestPost(url,null,params);
		return result;
	}
	public static String requestPost(String url, Map<String, String> headers, Map<String, Object> params)
	{
		String result = "";

		HttpRequest request = HttpRequest.post(url);
		if(null!=headers){
			for (Map.Entry<String, String> header : headers.entrySet()) {
				request.header(header.getKey(), header.getValue());
			}
		}

		result = request.form(params).execute().body();
		return result;
	}
	/**
	 * post请求中urlParams，追加到路径后面的
	 * @param url
	 * @param headers
	 * @param urlParams
	 * @param params
	 * @return
	 */
	public static String requestPostUrlParam(String url, Map<String, String> headers, Map<String, Object> urlParams, Map<String, Object> params)
	{
		String result = "";
		StringBuilder builder = new StringBuilder(url);
		builder.append("?");
		for (Map.Entry<String, Object> param : urlParams.entrySet()) {
			builder.append(param.getKey()).append("=").append(param.getValue()).append("&");
		}
		HttpRequest request = HttpRequest.post(builder.toString());
		for (Map.Entry<String, String> header : headers.entrySet()) {
			request.header(header.getKey(), header.getValue());
		}
		result = request.form(params).execute().body();
		return result;
	}
	
	/**
	 * post请求中urlParams，追加到路径后面的
	 * @param url
	 * @param params
	 * @return
	 */

	public static String requestPostBodyJSONParam(String url, Map<String, Object> params)
	{
		String result = requestPostBodyJSONParam(url,null,params);
		return result;
	}
	public static String requestPostUrlBodyJSONParam(String url, Map<String, Object> urlParams,Map<String,Object> formParams)
	{
		StringBuilder builder = new StringBuilder(url);
		builder.append("?");
		for (Map.Entry<String, Object> param : urlParams.entrySet()) {
			builder.append(param.getKey()).append("=").append(param.getValue()).append("&");
		}
		if(builder.toString().endsWith("&")){
			builder.deleteCharAt(builder.length()-1);
		}
		String result = requestPostBodyJSONParam(builder.toString(),null,formParams);
		return result;
	}
	public static String requestPostBodyJSONParam(String url, Map<String, String> headers,Map<String, Object> formParams)
	{
		String result = "";
		HttpRequest request = HttpRequest.post(url);
		if(headers!=null){
			for (Map.Entry<String, String> header : headers.entrySet()) {
				request.header(header.getKey(), header.getValue());
			}
		}

		String json=JSONObject.toJSONString(formParams);
		System.out.println("json="+json);
		result = request.body(json, "application/json").execute().body();
		return result;
	}
}

